/**
 * @file   PUTFILEAuthMsg.java
 * @date   2012-12-14下午5:18:04
 * @author user
 */
package com.gzceb.zhgjj.model;

import java.io.File;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gzceb.zhgjj.filecheck.impl.PutfileCheck;
import com.gzceb.zhgjj.util.Utils;
import com.gzceb.zhgjj.util.XMLUtils;

/**
 * <p>PUTFILE授权文件接口报文处理类。</p>
 *
 * @date   2012-12-14下午5:18:04 
 * @author liqing
 * @description 对接收到的PUTFILE报文做处理，并调用依赖，生成授权文件响应文件，生成反馈报文
 */
public class PUTFILEAuthMsg {
	private  final Log logger = LogFactory.getLog(PUTFILEAuthMsg.class);
	public String destPath;
	
	public PUTFILEAuthMsg(String destPath){
		this.destPath = destPath;
	}
	
	/**
	 * 处理6152端口接收到的授权报文，处理生成授权文件的反馈文件，返回授权响应报文
	 * @param message　String
	 * @return 响应报文
	 */
	public String ProcRcvMsg(String message){
		String rspStr="";
		logger.info("got the message: " + "\n" + message);
		Map<String, String> msgMap = XMLUtils.XMLParser(message);
		String rspFileName = "";
		String fileName = msgMap.get("wjm");
		// 除去路径，只取文件名
		fileName = Utils.getFileName(fileName);
		try{
			File file = new File(this.destPath + fileName);
			
			PutfileCheck pfCheck = new PutfileCheck(file);
			rspFileName = pfCheck.crtRespFile();
			
		}catch(Exception e){
			logger.error("error happended when create rsp file. \n" + e.getMessage());
			
		}
		
		rspStr = createRspMsg(rspFileName);		
		return rspStr;
		
	}
	
	
	/**
	 * 生成PUTFILE响应报文			
	 * @auth   		liqing
	 * @date 	Dec 17, 2012 5:20:25 PM 
	 * @param 
	 * 
	 */
	public String createRspMsg(String rspFileName){
		String rspCode;
		//TODO 需增加响应代码的判断
		if("".equals(rspFileName) && rspFileName.length() == 0){
			rspCode = Constants.RESPCODE_SUC;
		}else{
			rspCode = Constants.RESPCODE_FAIL;
		}
		
		String rspStr = "<root>"
		+"<xym>" + rspCode + "</xym>"
		+"<jydm>PUTFILE</jydm>"
		+"<jyrq>" + Utils.getCurrDateStr("yyyyHHdd") + "</jyrq>"
		+"<wjlx>4</wjlx>"
		+"<wjm>" + rspFileName + "</wjm>"
		+"</root>";
		
		return Utils.intToString(rspStr.length(), 4)+ rspStr;
		
	}
}
